/**
   Copyright 2011 Фроловский А. В.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This file is a part of MyStats project
 */

package foo.mystats.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbOpenHelper extends SQLiteOpenHelper {

  private static final int DB_VERSION = 2;
  private static final String DB_NAME = "mystats.db";

  /*
   * No foreign keys on Android 2.1 (SQLite < 3.6.19) :(
   */
  private static final String SQL_PRAGMA = "PRAGMA foreign_keys=ON;";

  public DbOpenHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }

  @Override
  public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
      db.execSQL(SQL_PRAGMA);
    }
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_PRAGMA);
    RowTable.onCreate(db);
    DataTable.onCreate(db);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_PRAGMA);
    RowTable.onUpgrade(db, oldVersion, newVersion);
    DataTable.onUpgrade(db, oldVersion, newVersion);
  }
}
